package main

import (
	"bytes"
	"fmt"
	"log"
	"os"
)

func main() {
	//testOne()
	buffStringTest()
}

func buffStringTest() {
	//学习链接：https://www.cnblogs.com/wangjq19920210/p/13051447.html
	buff := bytes.NewBufferString("123456789")
	log.Println("buff = ", buff.String())
	
	s := make([]byte, 4)
	n, _ := buff.Read(s)
	log.Println("buff = ", buff.String())
	log.Println("s = ", string(s))
	log.Println("n = ", n)
	
	n, _ = buff.Read(s)
	log.Println("buff2 = ", buff.String())
	log.Println("s2 = ", string(s))
	log.Println("n2 = ", n)
	
	n, _ = buff.Read(s)
	log.Println("buff3 = ", buff.String())
	log.Println("s3 = ", string(s))
	log.Println("n3 = ", n)
	
	buff.Reset()
	buff.WriteString("abcdefg")
	log.Println("buff = ", buff.String())
	
	//这里是读取缓冲区
	b, _ := buff.ReadByte()
	log.Println("b = ", string(b))
	log.Println("buff = ", buff.String()) //.String()是returns the contents of the unread portion of the buffer
	
	bs, _ := buff.ReadBytes('f')
	log.Println("bs = ", string(bs))
	
	buff.Reset()
	buff.WriteString("编译输出GO")
	r, l, _ := buff.ReadRune()
	log.Println("r = ", r, ", l = ", l, ", string(r) = ", string(r))
	
	buff.Reset()
	buff.WriteString("qwer")
	str, _ := buff.ReadString('?')
	log.Println("str = ", str)
	log.Println("buff = ", buff.String())
	
	buff.WriteString("qwer")
	str, _ = buff.ReadString('w')
	log.Println("str = ", str)
	log.Println("buff = ", buff.String())
	
	//从文件中获取字节缓冲区
	file, _ := os.Open("doc.go")
	buff.Reset()
	buff.ReadFrom(file)
	log.Println("doc.go = ", buff.String())
	
	buff.Reset()
	buff.WriteString("中国人")
	cbyte := buff.Bytes()
	log.Println("cbyte = ", cbyte)
}

func bufferStringTest() {
	buf := &bytes.Buffer{}
	buf.WriteString("abc?defg")
	
	str, _ := buf.ReadString('?')
	log.Println("str = ", str)
	log.Println("buff = ", buf.String())
}

func testOne() {
	byteSlice := make([]byte, 20)
	byteSlice[0] = 1                                  // 将缓冲区第一个字节置1
	byteBuffer := bytes.NewBuffer(byteSlice)          // 创建20字节缓冲区 len = 20 off = 0
	c, _ := byteBuffer.ReadByte()                     // off+=1
	fmt.Printf("len:%d, c=%d\n", byteBuffer.Len(), c) // len = 20 off =1   打印c=1
	byteBuffer.Reset()                                // len = 0 off = 0
	fmt.Printf("len:%d\n", byteBuffer.Len())          // 打印len=0
	byteBuffer.Write([]byte("hello byte buffer"))     // 写缓冲区  len+=17
	fmt.Printf("len:%d\n", byteBuffer.Len())          // 打印len=17
	byteBuffer.Next(4)                                // 跳过4个字节 off+=4
	fmt.Printf("len Truncate之前长度:%d\n", byteBuffer.Len())
	c, _ = byteBuffer.ReadByte()    // 读第5个字节 off+=1
	fmt.Printf("第5个字节:%d\n", c) // 打印:111(对应字母o)    len=17 off=5
	byteBuffer.Truncate(3)          // 将未字节数置为3        len=off+3=8   off=5
	fmt.Println("Truncate操作后：", byteBuffer.String())
	fmt.Printf("len:%d\n", byteBuffer.Len()) // 打印len=3为未读字节数  上面len=8是底层切片长度
	byteBuffer.WriteByte(96)                 // len+=1=9 将y改成A
	byteBuffer.Next(3)                       // len=9 off+=3=8
	c, _ = byteBuffer.ReadByte()             // off+=1=9    c=96
	fmt.Printf("第9个字节:%d\n", c)          // 打印:96
}

func testTwo() {
	data := []byte("路多辛的所思所想")
	reader := bytes.NewReader(data)
	
	// 读取字节切片的一部分
	part := make([]byte, 3)
	value, err := reader.Read(part)
	if err != nil {
		fmt.Println("Read failed:", err)
	}
	fmt.Println("Bytes read:", part, "value:为", value) //打印结果为——Bytes read: [232 183 175] value:为 3
	//232、183、175,如果是英文的，打印对应的是ASCII码表对应的值
}

func bufferReadoutFile() {
	file, _ := os.Create("text.txt")
	buf := bytes.NewBufferString("hello world")
	buf.WriteTo(file)
	//或者使用写入，fmt.Fprintf(file,buf.String())
}
